package com.ruoyi.web.controller.initData;

import com.ruoyi.system.domain.dao.SysPostDao;
import com.ruoyi.system.mapper.OaMapper;
import com.ruoyi.system.mapper.SysPostDaoMapper;
import com.ruoyi.tools.utils.pinyin.Pinyin4jUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
public class DutytInit {

    @Autowired
    private OaMapper oaMapper;
    @Resource
    private SysPostDaoMapper sysPostDaoMapper;

    /**
     * 初始化 岗位
     */
    public void initDuty2Post() {
        List<Map<String, String>> list = oaMapper.listData("ent_t_duty");
        Map<String, String> map = new HashMap<>();  // <depCode,id>，用于替换depId
        if (list != null && list.size() > 0) {
            list.stream().forEach(item -> {
                if ("0".equals(String.valueOf(item.get("is_valid")))) {
                    return;
                }
                SysPostDao sysPost = new SysPostDao();
                sysPost.setPostName(item.get("duty_name"));
                sysPost.setPostCode(Pinyin4jUtil.toPinyin(sysPost.getPostName(), false));
                sysPost.setPostSort(Integer.parseInt(String.valueOf(item.get("sort_num"))));
                sysPost.setStatus("0");
                sysPost.setOldData(String.format("dutyId=%s,dutyUuid=%s,depUuid=%s", item.get("duty_id"), item.get("ent_t_duty_uuid"), item.get("ent_t_dep_uuid")));

                sysPostDaoMapper.insert(sysPost);
            });
        }
    }

}
